import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from init import Matrix

def create_matrix_input():
    global entry_rows,entry_cols,frame_matrix,button_matrix
    rows=int(entry_rows.get())
    cols=int(entry_cols.get())
    frame_matrix=ttk.Frame(root,width=250,height=120,borderwidth=1,relief="solid")
    frame_matrix.pack(anchor="center",pady=20)
    for i in range(rows):
        for j in range(cols):
            global entry
            entry=tk.Entry(frame_matrix,width=10)
            entry.grid(row=i,column=j,padx=10,pady=10)
    def matrix_transposition():
        global a
        List=[[None for _ in range(cols)] for _ in range(rows)]
        for entry in frame_matrix.winfo_children():
            if isinstance(entry,tk.Entry):
                #判断输入内容是否为数字
                if not entry.get().isdigit():
                    messagebox.showinfo("结果","输入内容不为数字")
                    return
                #判断输入内容是否为空
                if entry.get()=="":
                    messagebox.showinfo("结果","输入内容为空")
                    return
                else :
                    #获取行列
                    row=entry.grid_info()["row"]
                    col=entry.grid_info()["column"]
                    List[row][col]=int(entry.get())
        a=Matrix(rows,cols,List)
        matrix_transposition_result(a);
    def matrix_transposition_result(a):
        global b
        #创建一个新的窗口
        root=tk.Toplevel()
        root.title("矩阵转置结果")
        root.geometry("500x500+400+200")
        root.resizable()
        #创建一个新的frame
        frame_result=ttk.Frame(root,width=250,height=120,borderwidth=1,relief="solid")
        frame_result.pack(anchor="center",pady=20)
        #获取转置矩阵
        result=a.matrix_transposition()
        #将转置矩阵显示在新的frame中
        for i in range(len(result)):
            for j in range(len(result[0])):
                label=tk.Label(frame_result,text=result[i][j],font=("黑体",10))
                label.grid(row=i,column=j,padx=10,pady=10)
        b=a.matrix_rank()
        label_rank=tk.Label(root,text="矩阵的秩为："+str(b),font=("黑体",10))
        label_rank.pack(anchor="center",pady=20)
    #得到矩阵的秩
    # def matrix_rank():
    #     window=tk.Toplevel()
    #     window.title("矩阵秩")
    #     window.geometry()
    #     window.resizable()
    #     label_rank=tk.Label(window,text="矩阵的秩为："+str(b),font=("黑体",10))
    #     label_rank.pack(anchor="center",pady=20)
    #     #创建一个按钮，点击后关闭窗口
    #     button_close=tk.Button(window,text="关闭",font=("黑体",10),command=window.destroy)
    #     button_close.pack(anchor="center",pady=20)

    #     #创建一个按钮，点击后关闭窗口
    #     button_close=tk.Button(root,text="关闭",font=("黑体",10),command=root.destroy)
    #     button_close.pack(anchor="center",pady=20)


    button_matrix=tk.Button(root,text="生成转置矩阵",font=("黑体",10),command=matrix_transposition)
    button_matrix.pack(anchor="center",pady=0)

    # button_rank=tk.Button(root,text="生成矩阵秩",font=("黑体",10),command=matrix_rank)
    # button_rank.pack(anchor="center",pady=0)


root=tk.Tk()
root.title("矩阵计算")
root.geometry("500x500+400+200")
root.resizable()

label_title=tk.Label(root,text="矩阵计算",font=("黑体",20))
label_title.pack(anchor="center",pady=20)

frame_main=ttk.Frame(root,width=250,height=120,borderwidth=1,relief="solid")
frame_main.pack(anchor="center",pady=20)

label_rows=tk.Label(frame_main,text="行数",font=("黑体",10))
label_rows.grid(row=0,column=0,padx=10,pady=10)

label_cols=tk.Label(frame_main,text="列数",font=("黑体",10))
label_cols.grid(row=0,column=1,padx=10,pady=10)

entry_rows=tk.Entry(frame_main,width=10)
entry_rows.grid(row=1,column=0,padx=10,pady=10)

entry_cols=tk.Entry(frame_main,width=10)
entry_cols.grid(row=1,column=1,padx=10,pady=10)

#设计一个界面全部清空的按钮
def clear():
    entry_rows.delete(0,END)
    entry_cols.delete(0,END)
    frame_matrix.destroy()
    button_matrix.destroy()



button_create=tk.Button(root,text="创建矩阵",font=("黑体",10),command=create_matrix_input)
button_create.pack(anchor="center",pady=0)

button_clear=tk.Button(root,text="清空",font=("黑体",10),command=clear,width=10)
button_clear.pack(anchor="center",pady=(20,20))


if __name__=="__main__":
    root.mainloop()